<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>角色管理</title>
<link th:include="common/header::copy"/>

    <script type="text/javascript">

	function formatOp(val,row,index){
        var str = '';
        if ($("#rolemenuset").length > 0) {
            str += $.formatString('<a  href="javascript:void(0)" class="res-easyui-linkbutton-edit"  onclick="menuSetFun(\'{0}\');" >资源设置</a>', row.rid);
        }
        if ($("#roleupdate").length > 0) {
            str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-edit"  onclick="editRoleFun(\'{0}\');" >编辑</a>', index);
        }
        if ($("#roledelete").length > 0) {
            // str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del"   onclick="deleteData(\'/sys/role/delete\',roleid, \'{0}\', \'删除成功！\');" >删除</a>', row.rid);            str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del"   onclick="deleteData(\'/sys/role/delete\',roleid, \'{0}\', \'删除成功！\');" >删除</a>', row.rid);
            str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del"   onclick="deleteRoleFun(\'{0}\');" >删除</a>', row.rid);
        }
        return str;
		// return "<a href=\"javascript:openMenuSetDialog("+row.rid+")\"><img style='margin-top:1px' src='/images/edit.gif'/></a>";
	}
//保存的时候有一点Bug,选子节点时不选父节点，则会跳过父节点及其子节点，这是不合理的
	function menuSetFun(roleId){
		$("#dlg2").dialog("open").dialog("setTitle","权限菜单设置");
		$("#menuTree").tree({
			lines:true,
			checkbox:true,
			cascadeCheck:false,
			url:'/sys/res?roleid='+roleId,
            method:"post",
			onLoadSuccess:function(){
				$("#menuTree").tree("expandAll");
			},
		});
		$("#roleid").val(roleId);
	}

	function saveMenuSet(){
		var roleid=$("#roleid").val();
        var nodes=$("#menuTree").tree("getChecked");
        var menuArrIds=[];
		for(var i=0;i<nodes.length;i++){
			menuArrIds.push(nodes[i].id);
		}
		var menuIds=menuArrIds.join(",");
		$.post("/sys/role/setroleres",{resids:menuIds,roleid:roleid},function(result){
			if(result.success){
				closeDialog('#dlg2');
			}else{
				$.messager.alert("系统提示","提交失败，请联系管理员");
			}
		},"json");
	}

	// function closeMenuSetDialog(){
	// 	$("#dlg2").dialog("close");
	// }

	function searchRole(){
		$("#dg").datagrid('load',{
			"rolename":$("#s_roleName").val()
		});
	}

	function resetValue(){
	    $("#rid").val("");
		$("#name").val("");
		$("#remark").val("");
	}

	function openRoleAddDialog(){
		$("#dlg").dialog("open").dialog("setTitle","添加角色信息");
	}
    function editRoleFun(index){
        $("#dg").datagrid('selectRow',index);
        var row =$("#dg").datagrid('getSelected');
		$("#dlg").dialog("open").dialog("setTitle", "修改角色信息");
		$("#fm").form("load", row);
    }

    function deleteRoleFun(rid){
		$.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
			if(r){
				$.post("/sys/role/delete",{roleid:rid},function(result){
					if(result.success){
						show("删除成功!");
						$("#dg").datagrid("reload");
					}else{
						show(result.info);
					}
				},"json");
			}
		});
	}

</script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="sys:role:menuset">
    <input type="hidden" id="rolemenuset"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:role:delete">
    <input type="hidden" id="roledelete"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:role:save">
    <input type="hidden" id="roleupdate"/>
</shiro:hasPermission>
	<table id="dg" title="角色管理" class="easyui-datagrid"
		   data-options="fit:true,fitColumns:true,pagination:true,rownumbers:true,
		   singleSelect:true,url:'/sys/role/list',method:'post',toolbar:'#tb'">
		<thead>
			<th field="rid" width="20" align="center" hidden>编号</th>
			<th field="rname" width="50" align="center">角色名称</th>
			<th field="remark" width="100" align="center">备注</th>
			<th field="op" width="50" align="center" formatter="formatOp">权限菜单设置</th>
		</thead>
	</table>

	<div id="tb">
		<div shiro:hasPermission="sys:role:add" display= "inline-block" >
			 <a href="javascript:openRoleAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
		</div>
		<div shiro:hasPermission="sys:role:list" style="float:right;" display="inline-block">
			&nbsp;角色名称：&nbsp;<input type="text" id="s_roleName" size="20" onkeydown="if(event.keyCode==13) searchRole()"/>
			<a href="javascript:searchRole()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>

	<div id="dlg" class="easyui-dialog" style="width: 450px;height: 250px;padding: 10px 20px"
	closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
		<form id="fm" method="post">
            <input type="hidden" id="rid" name="rid">
			<table cellspacing="8px">
				<tr>
					<td>角色名称：</td>
					<td>
						<input type="text" id="name" name="rname" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
				<tr>
					<td valign="top">备注：</td>
					<td>
						<textarea rows="5" cols="40" id="remark" name="remark"></textarea>
					</td>
				</tr>
			</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:save('/sys/role/save','保存成功！');" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
		<a href="javascript:closeDialog('#dlg');" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
	</div>


	<div id="dlg2" class="easyui-dialog" style="width: 300px;height: 500px;padding: 10px 20px"
	closed="true" buttons="#dlg-buttons2">
		<input type="hidden" id="roleid" name="roleid">
		<ul id="menuTree" class="easyui-tree"></ul>
	</div>

	<div id="dlg-buttons2">
		<a href="javascript:saveMenuSet()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
		<a href="javascript:closeDialog('#dlg2')" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
	</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.res-easyui-linkbutton-edit').linkbutton({text: '资源设置', plain: true, iconCls: 'icon-tip'});
                $('.role-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'icon-edit'});
                $('.role-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'icon-remove'});
				$('#dg').datagrid('fixRowHeight');
            }
        });
    })
</script>
</body>
</html>